<?php

namespace app\common\model\goods;

use app\common\model\UserGroup;
use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'type_text',
        'hot_text',
        'meal_text',
        'min_price',
        'image_str',
        'images_array',
        'spec_json_text',
    ];
    public function getImageStrAttr($v, $d)
    {
        return $d['image'] ? cdnurl($d['image'], true) : '';
    }
    public function getImagesArrayAttr($v, $d)
    {
        if ($d['images']) {
            $array = explode(',', $d['images']);
            foreach ($array as &$value) {
                $value = $value ? cdnurl($value, true) : '';
            }
            return $array;
        }
        return [];
    }
    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }
    public function getStockAttr($v, $d)
    {

        return  Spec::where('goods_id', $d['id'])->sum('stock');
    }

    public function getSpecJsonTextAttr($value, $data)
    {

        return  json_encode(Spec::all(['goods_id' => $data['id']]), true);
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }

    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getHotList()
    {
        return ['0' => __('Hot 0'), '1' => __('Hot 1')];
    }

    public function getMealList()
    {
        return ['0' => __('Meal 0'), '1' => __('Meal 1')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getHotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['hot']) ? $data['hot'] : '');
        $list = $this->getHotList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getMinPriceAttr($v, $d)
    {
        return Spec::where('goods_id', $d['id'])->min('original_price');
    }


    public function getMealTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['meal']) ? $data['meal'] : '');
        $list = $this->getMealList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    function group()
    {
        return $this->belongsTo(UserGroup::class, 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    function spec()
    {
        return $this->hasMany(Spec::class, 'goods_id', 'id');
    }
    function category()
    {
        return $this->belongsTo(Category::class, 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
